//
//  VRProfileTopView.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/13.
//

import UIKit

class VRProfileTopView: UIView {
    
    private let bgImageView         = c_make_ex(UIImageView.self)
        .image("Profile_topBg").mode(.scaleAspectFill).end()
    private let idLabel          = c_make_ex(UILabel.self)
        .color(.white).font(14.font_medium)
        .text("ID：89794466").end()
    private let settingBtn          = c_make_ex(UIButton.self)
        .image("Profile_setting").end()
    private let nameLabel          = c_make_ex(UILabel.self)
        .color(.white).font(22.font_bold)
        .text("").end()
    private let genderBtn          = c_make_ex(UIButton.self)
        .image("Profile_male").bgColor("#F72AFF").corner(10).end()
    private let ageLabel           = c_make_ex(LabelWithPadding.self)
        .color(.black).font(14.font_semiBold).bgColor("#FFFC00")
        .text("24").corner(10).end()
    private let subLabel            = c_make_ex(UILabel.self)
        .color(.white.alpha(0.4)).font(12.font_medium)
        .line(0)
        .text("Choose your gender and let more\npeople know you").end()
    private let iconImageView         = c_make_ex(UIImageView.self)
        .image("welcome_avatar").mode(.scaleAspectFill).corner(30).end()
    private let editButton          = c_make_ex(UIButton.self)
        .image("Profile_edit").end()
    private let followerNumLab          = c_make_ex(UILabel.self)
        .color(.white).font(22.font_medium).alignment(.center)
        .text("24").end()
    private let followerLab          = c_make_ex(UILabel.self)
        .color(.white.alpha(0.56)).font(12.font_medium).alignment(.center)
        .text("Follower").end()
    private let followerBtn          = c_make_ex(UIButton.self)
        .bgColor(.clear).end()
    private let followNumLab          = c_make_ex(UILabel.self)
        .color(.white).font(22.font_medium).alignment(.center)
        .text("24").end()
    private let followLab          = c_make_ex(UILabel.self)
        .color(.white.alpha(0.56)).font(12.font_medium).alignment(.center)
        .text("Follow").end()
    private let followBtn          = c_make_ex(UIButton.self)
        .bgColor(.clear).end()
    private let coinButton          = c_make_ex(UIButton.self)
        .image("Profile_coins").end()
    private let coinLabel          = c_make_ex(UILabel.self)
        .color(.black).font(12.font_semiBold)
        .text("Coins").end()
    private let numberLabel          = c_make_ex(UILabel.self)
        .color(.black).font(24.font_bold)
        .text("225").end()
    
//    var settingBlock: (() -> Void)?
//    var followerBlock: (() -> Void)?
//    var followBlock: (() -> Void)?
    func updateUI() {
        
        if let headImg = USEINFOMODEL.headImage {
            self.iconImageView.loadImage(url: headImg)
        }else{
            self.iconImageView.image = "welcome_avatar".toImage
        }
        if let name = USEINFOMODEL.name {
            self.nameLabel.text = name
        }else{
            self.nameLabel.text = "Scholar"
        }
        if let ID = USEINFOMODEL.id {
            self.idLabel.text = "ID：" + ID
        }else{
            self.idLabel.text = "ID：44556782"
        }
        if let coin = USEINFOMODEL.coins {
            self.numberLabel.text = coin
        }
        if let bgImg = USEINFOMODEL.photo {
            self.bgImageView.loadImage(url: bgImg)
        }else{
            self.bgImageView.image = "Profile_topBg".toImage
        }
        if let gender = USEINFOMODEL.gender {
            if gender == "0" {
                genderBtn.setImage("Profile_male".toImage, for: .normal)
            }else{
                genderBtn.setImage("Profile_female".toImage, for: .normal)
            }
        }
        
        if let sign = USEINFOMODEL.info1 {
            self.subLabel.text = sign
        }else{
            self.subLabel.text = "Choose your gender and let more\npeople know you"
        }
    }
    
    var followerCount: Int = 0 {
        didSet{
            followerNumLab.text = String(followerCount)
        }
    }
    
    var followingCount: Int = 0 {
        didSet{
            followNumLab.text = String(followingCount)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            bgImageView
            idLabel
            settingBtn
            nameLabel
            genderBtn
            ageLabel
            subLabel
            iconImageView
            editButton
            followerNumLab
            followerLab
            followerBtn
            followNumLab
            followLab
            followBtn
            coinButton.subviews{
                coinLabel
                numberLabel
            }
        }
        
        bgImageView.fillContainer()
        idLabel.leading(15).top(.statusHeight + 12)
        settingBtn.trailing(10).top(.statusHeight).size(40)
        nameLabel.leading(15)
        nameLabel.Top == settingBtn.Bottom + 17
        genderBtn.size(20)
        genderBtn.Leading == nameLabel.Trailing + 5
        genderBtn.CenterY == nameLabel.CenterY
        ageLabel.Leading == genderBtn.Trailing + 8
        ageLabel.CenterY == genderBtn.CenterY
        subLabel.leading(15)
        subLabel.Top == nameLabel.Bottom + 10
        iconImageView.size(60).trailing(16)
        iconImageView.Top == settingBtn.Bottom + 16
        subLabel.Trailing == iconImageView.Leading - 20
        editButton.trailing(15).size(40)
        editButton.Top == iconImageView.Bottom + 32
        coinButton.leading(15).trailing(15).bottom(15).height(50)
        coinLabel.leading(45).centerVertically()
        numberLabel.leading(85.5).centerVertically()
        followerLab.leading(15)
        followerLab.Bottom == coinButton.Top - 25
        followerNumLab.Bottom == followerLab.Top
        followerNumLab.CenterX == followerLab.CenterX
        followerBtn.leading(15)
        followerBtn.Trailing == followerLab.Trailing
        followerBtn.Top == followerNumLab.Top
        followerBtn.Bottom == followerLab.Bottom
        followLab.leading(100)
        followLab.Bottom == coinButton.Top - 25
        followNumLab.Bottom == followLab.Top
        followNumLab.CenterX == followLab.CenterX
        followBtn.leading(100)
        followBtn.Trailing == followLab.Trailing
        followBtn.Top == followNumLab.Top
        followBtn.Bottom == followLab.Bottom
        
        genderBtn.isUserInteractionEnabled = false
        ageLabel.padding = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
        iconImageView.isUserInteractionEnabled = true
        
        let tapG = UITapGestureRecognizer(target: self, action: #selector(clickEditButton))
        iconImageView.addGestureRecognizer(tapG)
        editButton.addTarget(self, action: #selector(clickEditButton), for: .touchUpInside)
        settingBtn.addTarget(self, action: #selector(clickSettingButton), for: .touchUpInside)
        followerBtn.addTarget(self, action: #selector(clickFollowerButton), for: .touchUpInside)
        followBtn.addTarget(self, action: #selector(clickFollowButton), for: .touchUpInside)
        coinButton.addTarget(self, action: #selector(clickCoinButton), for: .touchUpInside)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: action
extension VRProfileTopView {
        
    @objc private func clickSettingButton(){
//        currentViewController().navigationController?.pushViewController(VRSettingViewController(), animated: true)
        print(USEINFOMODEL.gender)
        
        
    }
    
    @objc private func clickFollowerButton(){
        let follower = VRFollowViewController()
        follower.type = 2
        currentViewController().navigationController?.pushViewController(follower, animated: true)
    }
    
    @objc private func clickFollowButton(){
        let follow = VRFollowViewController()
        follow.type = 1
        currentViewController().navigationController?.pushViewController(follow, animated: true)
    }
    
    @objc private func clickCoinButton(){
        currentViewController().navigationController?.pushViewController(VRRechargeViewController(), animated: true)
    }
    
    @objc private func clickEditButton(){
        currentViewController().navigationController?.pushViewController(VREditViewController(), animated: true)
    }
    
}
